package com.trkj.controller;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import org.apache.commons.fileupload.FileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.trkj.pojos.CcPrepayment;
import com.trkj.service.CcHospEleCheckService;
import com.trkj.service.CcOuthospitalService;
import com.trkj.vo.CcElectornicCheckVo;
import com.trkj.vo.Result;

@Controller
public class CcHospEleCheckController {

	@Autowired
	private CcHospEleCheckService chospcervice;
	@Autowired
	private CcOuthospitalService outhospService;
	
	@RequestMapping("/tocc_hospelecheckok")
	public void upload(@RequestParam("file") CommonsMultipartFile file,
            HttpServletRequest request,HttpServletResponse response)
            		throws IOException, ServletException{
			//获取路径
			ServletContext sc = request.getServletContext();
			String lj=sc.getRealPath("/WEB-INF/photos");
			System.err.println(lj);
			//创建文件夹
			File fi=new File(lj);
			if(!fi.exists()){
		         fi.mkdirs();//如果不存在就创建文件夹
		    }
			if(file!=null){
		        //获取文件的原始名称
				String fileName=file.getOriginalFilename();
				//获取最后一个.的位置
				int i=fileName.lastIndexOf(".");
				//获取原始文件的扩展名
				String ext=fileName.substring(i);
				//生成一个全球唯一的字符串
		        String newFileName = UUID.randomUUID().toString().toUpperCase();
		        //拼成完整的文件名
		        newFileName=newFileName+ext;
		        File f=new File(fi,newFileName);
		        try {
		        	//转存...
					file.transferTo(f);
				} catch (IllegalStateException | IOException e) {
					e.printStackTrace();
				}
		        String checkid=request.getParameter("elecheckid");
				chospcervice.updateBycheckid(checkid, "/HIS/photos/"+newFileName);
		        //return  new Result(true,"操作成功！");
		    }
		
	}

	@RequestMapping("/cc_toqueryByAllEleCheck")
	public @ResponseBody Map<String,Object> queryByAll(int page,int rows){
		return chospcervice.queryByAll(page, rows);
	}
	@RequestMapping("/cc_toqueryByEleCheckicid")
	public @ResponseBody List<CcElectornicCheckVo> queryEleCheckicid(String icid){
		return chospcervice.queryByicid(icid);
	}
	
	@RequestMapping("/cc_toqueryByAllOuthosp")
	public @ResponseBody Map<String,Object> queryByAllOuthosp(int page,int rows){
		return outhospService.queryAllOuthosp(page, rows);
	}
	@RequestMapping("/cc_toqueryByOuthosp")
	public @ResponseBody CcPrepayment queryByOuthosp(String outid){
		return outhospService.queryByoutid(outid);
	}
	@RequestMapping("/tocc_updateoutHosp")
	public @ResponseBody Result updateoutHosp(String outid,String outDoctorid){
		outhospService.updateCcOuthosp(outid, outDoctorid);
		return  new Result(true,"操作成功！");
	}
	@RequestMapping("/tocc_updateOkoutHosp")
	public @ResponseBody Result updateOkoutHosp(String outid,String pid,String outDoctorid){
		outhospService.upOkCcOuthospital(outid, outDoctorid, pid);
		return  new Result(true,"操作成功！");
	}
}
